package com.jiaozi.core.security.utils;


import com.jiaozi.base.utils.bean.enums.REnum;
import com.jiaozi.core.exception.CommonException;
import com.jiaozi.core.security.pojo.LoginUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 获取登录用户信息的帮助类
 */
@Component
public class SecurityUtil {
    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户信息
     **/
    public static LoginUserDetails getLoginUser() {
        try {
            return (LoginUserDetails) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new CommonException(REnum.LOGIN_TOKEN_ERROR, "获取用户信息异常");
        }
    }




}
